package com.hjq.mallproduct.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hjq.mallproduct.entity.AttrEntity;
import com.hjq.mallproduct.entity.vo.AttrGroupVO;
import com.hjq.mallproduct.entity.vo.AttrRelationVO;
import com.hjq.mallproduct.service.AttrAttrgroupRelationService;
import com.hjq.mallproduct.service.CategoryService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hjq.mallproduct.entity.AttrGroupEntity;
import com.hjq.mallproduct.service.AttrGroupService;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.R;



/**
 * 属性分组
 *
 * @author Hjq
 * @email 1484397309@qq.com.com
 * @date 2022-05-24 14:18:33
 */
@RestController
@RequestMapping("mallproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @RequestMapping("/{catelogId}/withattr")
    public R getAllGroupWithAttr(@PathVariable("catelogId") Long catId) {
        List<AttrGroupVO> allGroupWithAttr = attrGroupService.getAllGroupWithAttr(catId);


        return R.ok().put("data",allGroupWithAttr);
    }

    @RequestMapping("/attr/relation")
    public R listNotAttr(@RequestBody List<AttrRelationVO> attrRelationVOList) {
        attrAttrgroupRelationService.saveAttrRelationBatch(attrRelationVOList);

        return R.ok();


    }
    ///product/attrgroup/{attrgroupId}/noattr/relation
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R listNotAttr(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrGroupId) {
        PageUtils page = attrGroupService.queryPageOfNotInAttrGroup(params,attrGroupId);

        return R.ok().put("page", page);


    }

        /**
         * 删除当前组的某个属性
         */
    @RequestMapping("/attr/relation/delete")
    //@RequiresPermissions("mallproduct:attrgroup:list")
    public R deleteAttrRelation(@RequestBody List<AttrRelationVO> attrRelationVOList){
        attrGroupService.deleteAttrRelation(attrRelationVOList);

        return R.ok();
    }
    /**
     * 获取当前组的所有属性
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("mallproduct:attrgroup:list")
    public R list1(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> page = attrGroupService.queryAllAttrPage(attrGroupId);

        return R.ok().put("data", page);
    }

//    @RequestMapping("/list/{catId}")
//    //@RequiresPermissions("mallproduct:attrgroup:list")
//    public R listWithCatId(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId ){
//        //PageUtils page = attrGroupService.queryPage(params);
//        PageUtils page = attrGroupService.queryPage(params,catId);
//
//        return R.ok().put("page", page);
//    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("mallproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("mallproduct:attrgroup:list")
    public R listWithCatId(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId ){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("mallproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        //AttrGroupEntity attrGroup = attrGroupService.getByIdIncludingPath(attrGroupId);
        List<Long> categoryPath = categoryService.getCategoryPath(catelogId);
        Long[] array = categoryPath.toArray(new Long[categoryPath.size()]);
        attrGroup.setCatelogPath(array);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("mallproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("mallproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("mallproduct:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
